import json
import logging

from django.http import JsonResponse
from wxcloudrun.models import requestList

logger = logging.getLogger('log')

def patientRequest(request, _):
    """
    获取当前计数

     `` request `` 请求对象
    """
    rsp = JsonResponse({'code': 0, 'errorMsg': ''}, json_dumps_params={'ensure_ascii': False})
    try:
        if request.method == 'GET' or request.method == 'get':
            rsp=JsonResponse({'code': -1, 'errorMsg': '错误的请求方式'}, json_dumps_params={'ensure_ascii': False})
        elif request.method == 'POST' or request.method == 'post':
            rsp = updateRequest(request)
        else:
            rsp = JsonResponse({'code': -1, 'errorMsg': '请求方式错误'},
                                json_dumps_params={'ensure_ascii': False})
    except Exception as e:
        rsp = JsonResponse({'code': -1, 'errorMsg': str(e)}, json_dumps_params={'ensure_ascii': False})
    logger.info('response result: {}'.format(rsp.content.decode('utf-8')))
    return rsp



def updateRequest(request):
    body = request.body
    body = json.loads(str(body, 'utf-8'))
    b = requestList()
    b.reship = body['reship']
    b.name = body['name']
    b.cardType=body['cardType']
    b.cardId = body['cardId']
    b.phoneNumber=body['phoneNumber']
    b.temp=body['temp']
    b.date=body['date']
    b.selected=str(body['selected'])
    b.signature=body['signature']
    b.save()
    rsp = JsonResponse({'code': 0, 'date': '传送成功'},
                       json_dumps_params={'ensure_ascii': False})

    return rsp